/**
 * 模拟实现LinkList
 */

public class MyLinkList {
    private class ListNode{
        int val;
        ListNode next;
        ListNode prev;
        ListNode(int val){
            this.val = val;
        }
    }
    private ListNode head;
    private ListNode tail;

    public MyLinkList() {
    }

    public void display(){
        ListNode cur = head;
        while(cur != null){
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }

    //链表长度
    public int size(){
        ListNode cur = head;
        int count = 0;
        while(cur != null){
            count++;
            cur = cur.next;
        }
        return count;
    }

    //判断链表中是否包含某元素
    public boolean contain(int val){
        ListNode cur = head;
        while(cur != null){
            if(cur.val == val){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //头插法
    public void addFirst(int data){
        if(head == null){
            head = new ListNode(data);
            tail = head;
            return;
        }
        ListNode newNode = new ListNode(data);
        newNode.next = head;
        head.prev = newNode;
        head = newNode;
    }

    //尾插法
    public void addLast(int data){
        if(head == null){
            head = new ListNode(data);
            tail = head;
            return;
        }
        ListNode newNode = new ListNode(data);
        tail.next = newNode;
        newNode.prev = tail;
        tail = newNode;
    }


    //指定位置插入元素 head逻辑下标为0
    public void insert(int index,int data){
        //先判断index插入的位置是否合法
        if(index < 0 || index > size()){
            throw new IndexException("index 插入位置不合法！");
        }
        //头插
        if(index == 0){
            addFirst(data);
            return;
        }
        //尾插
        if(index == size()){
            addLast(data);
            return;
        }
        //常规插入
        ListNode cur = head;
        for (int i = 0;i < index;i++){
            cur = cur.next;
        }
        ListNode newNode = new ListNode(data);
        newNode.next = cur;
        newNode.prev = cur.prev;
        cur.prev.next = newNode;
        cur.prev = newNode;
    }

    //删除第一次出现的关键字为key的节点
    public void remove(int key){
        ListNode cur = head;
        while (cur != null){
            if(cur.val == key){
                //头结点为要删除的节点
                if(cur == head){
                    //只有一个头节点
                    if(head.next == null){
                        head = null;
                        tail = null;
                        return;
                    }
                    head = head.next;
                    head.prev = null;
                    return;
                }
                //尾节点为要删除的节点
                if(cur == tail){
                    tail = tail.prev;
                    tail.next = null;
                    return;
                }
                cur.prev.next = cur.next;
                cur.next.prev = cur.prev;
                return;
            }
            cur = cur.next;

        }
    }

    //删除所有关键字为key的节点
    public void removeAll(int key){
        ListNode cur = head.next;
        while(cur != null){
            if(cur.val == key){
                if(cur != tail){
                    cur.prev.next = cur.next;
                    cur.next.prev = cur.prev;
                }else {
                    tail = tail.prev;
                    tail.next = null;
                }
            }
            cur = cur.next;
        }
        if(head.val == key){
            //只有一个头结点
            if(head.next == null){
                head = null;
                tail = null;
            }else{
                //还有后继节点
                head = head.next;
                head.prev = null;
            }
        }
    }

    //清空链表
    public void clean(){
        ListNode cur = head;
        while(cur != null){
            ListNode curNext = cur.next;
            cur.next = null;
            cur.prev = null;
            cur = curNext;
        }
        //释放头结点
        head = null;
        tail = null;
    }
}
